﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Mirkuz.Mapping;

namespace Mirkuz.TrailCompanion
{
    public partial class fWaypoints : Form
    {
        public fWaypoints()
        {
            InitializeComponent();
        }
   
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            fAddWaypoint f = new fAddWaypoint(fMain.CurrentPosition);
            if (f.ShowDialog() == DialogResult.OK)
            {
                //add to the list
                AddWp(f.P);
                fMain.Wpmgr.Waypoints.Add(f.P);
            }
            f.Dispose();
            f = null;
        }

        private void cmdRemove_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count == 0)
            {
                return;
            }

            if ((MessageBox.Show("Delete selected waypoint?", "Confirm delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes))
            {
                int id = listView1.SelectedIndices[0];
                ListViewItem li=listView1.Items[id];

                foreach (WayPoint p in fMain.Wpmgr.Waypoints)
                {
                    if (p.Name == li.Text)
                    {
                        fMain.Wpmgr.Waypoints.Remove(p);
                        break;
                    }
                }

                listView1.Items.Remove(li);
            }

        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void fWaypoints_Load(object sender, EventArgs e)
        {
            WayPoint target = fMain.Trail.TargetWaypoint;

            UpdateLv();

            if (target != null)
            {
                foreach(ListViewItem li in listView1.Items)
                    if (li.Text==target.Name){
                        li.Selected = true;
                            break;
                    }
            }
        }

        private void UpdateLv()
        {
            listView1.SuspendLayout();
          
            foreach (WayPoint p in fMain.Wpmgr.Waypoints)
            {
                bool found = false;
                foreach (ListViewItem li in listView1.Items)
                {
                    if (p.Name == li.Text)
                    {
                        //update element
                        if (fMain.CurrentPosition != null)
                        {
                            li.SubItems[1].Text = p.SurfaceDistanceMeters(fMain.CurrentPosition).ToString("N0") + "m";
                            li.SubItems[2].Text = (p.Altitude - fMain.CurrentPosition.Altitude).ToString("N0") + "m";
                        }                        
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    AddWp(p);
                }             
            }

            listView1.ResumeLayout();
        }

        private void AddWp(WayPoint p)
        {
            //add element
            ListViewItem li = new ListViewItem(p.Name);

            if (fMain.CurrentPosition != null)
            {
                li.SubItems.Add(p.SurfaceDistanceMeters(fMain.CurrentPosition).ToString("N0") + "m");
                li.SubItems.Add((p.Altitude - fMain.CurrentPosition.Altitude).ToString("N0") + "m");
            }
            else
            {
                li.SubItems.Add("-");
                li.SubItems.Add("-");
            }

            listView1.Items.Add(li);
        }
        private void tmrUpdate_Tick(object sender, EventArgs e)
        {
            UpdateLv();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {              
                fMain.Wpmgr.SaveToKMLFile(saveFileDialog1.FileName);
            }
        }

        private void mnuLoad_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fMain.Wpmgr.LoadFromKMLFile(openFileDialog1.FileName);
                UpdateLv();
            }
        }

        private void mnuClear_Click(object sender, EventArgs e)
        {
            fMain.Wpmgr.Waypoints.Clear();
            listView1.Items.Clear();
            UpdateLv();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices == null || listView1.SelectedIndices.Count == 0)
            {
                fMain.Trail.SetTarget(null);
                return;
            }

            string selname = listView1.Items[listView1.SelectedIndices[0]].Text;

            foreach (WayPoint p in fMain.Wpmgr.Waypoints)
                if (p.Name == selname)
                {
                    fMain.Trail.SetTarget(p);
                    break;
                }
        }


    }
}